package kingwing.dc.core.view.frame.main.register.menu;

import java.util.Collection;
import java.util.List;

import kingwing.dc.core.model.MyUrls;

import org.hibernate.Session;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.CoreHibernateDao;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.model.Url;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.bdf2.core.view.frame.main.MainFrame;

@Component
public class SystemInitMenuController extends CoreHibernateDao implements
		InitializingBean, ApplicationContextAware {
	
	private ApplicationContext applicationContext;

	@Override
	public void afterPropertiesSet() throws Exception {
		
		this.doInHibernateSession(new ISessionCallback<Object>() {

			@Override
			public Object doInSession(Session session) {
				
				Number num = (Number) session.createQuery(
						"select count(*) from " + Url.class.getName())
						.uniqueResult();
				
				if (num.intValue() == 0) {
					String companyId = getFixedCompanyId();
					String rootId = "root-" + companyId;
					initRoot(rootId, companyId);
					Collection<AbstractMenuGenerator> generators = applicationContext
							.getBeansOfType(AbstractMenuGenerator.class)
							.values();
					if (generators != null) {
						for (AbstractMenuGenerator amg : generators) {
							List<Url> urls = amg.getUrls(rootId);
							if (urls != null && urls.size() > 0) {
								for (Url url : urls) {
									session.save(url);
								}
							}
						}
					}
				}
				
				return null;
			}
		});
		
		MainFrame frame = ContextHolder.getBean(MainFrame.BEAN_ID);
		frame.cacheNavigatorUrls();

	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		super.setApplicationContext(applicationContext);
		this.applicationContext = applicationContext;
	}
	
	private void initRoot(String rootId, String companyId) {
		Session session = this.getSessionFactory().openSession();
		try {
			String hql = "from " + Url.class.getName() + " where id=:rootId";
			Url url = (Url) session.createQuery(hql)
					.setParameter("rootId", rootId).uniqueResult();
			
			String hql2 = "from " + MyUrls.class.getName() + " where id=:rootId";
			MyUrls myUrl = (MyUrls) session.createQuery(hql2)
					.setParameter("rootId", rootId).uniqueResult();
			
			if (url == null) {
				Url root = new Url();
				root.setId(rootId);
				root.setCompanyId(companyId);
				root.setForNavigation(true);
				root.setName("系统管理");
				root.setIcon("url(skin>common/icons.gif) -102px -101px");
				root.setOrder(1);
				session.save(root);
			}
			if (myUrl == null) {
				MyUrls myRoot = new MyUrls();
				myRoot.setId(rootId);
				myRoot.setCompanyId(companyId);
				myRoot.setForNavigation(true);
				myRoot.setName("站点列表");
				myRoot.setIcon("url(skin>common/icons.gif) -102px -101px");
				myRoot.setOrder(2);
				session.save(myRoot);
			}
			
			
		} finally {
			if (session != null) {
				session.flush();
				session.close();
			}
		}

	}

}
